<?php

session_start();
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/config/mblog_config.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/core/mblog/weibooauth.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/mblog/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/love/inc.php");
error_reporting(E_ERROR | E_PARSE);

//error_reporting(E_ALL);
//所有的参数
$g_cgival 	= array();
//存储中间数据 非输出数据 可以不用
$g_pro	 	= array();
//要显示的数据 输出数据
$g_show 	= array();

function check_cgi_pro()
{		
	global $g_cgival, $g_pro, $g_show, $config;
	cgi::both($g_cgival["request_id"], "request_id", "");
	$g_cgival["request_id"] = intval($g_cgival["request_id"]);
	if(!$g_cgival["request_id"])
	{
		throw new Exception("request_id呢?");
	}

	cgi::both($g_cgival["mid"], "mid", "");
	
	cgi::both($g_cgival["open_id"], "open_id", "");
	
	cgi::both($g_cgival["content"], "content", "");
	
}


function get_data()
{
	global $g_cgival, $g_pro, $g_show, $config;

	if(!$_SESSION['last_key']['oauth_token'])
	{
		header("location:/sina_api/?url=/love/index.php");
		exit;
	}
	$c = new WeiboClient( WB_AKEY , WB_SKEY , $_SESSION['last_key']['oauth_token'] , $_SESSION['last_key']['oauth_token_secret']  );
	$current_user_info  = $c->verify_credentials(); // done
	$current_uid = $current_user_info["id"];
	$current_nick = $current_user_info["name"];
	
	
	$text = $g_cgival["content"];
		
	$db_love_bless = new db_love_bless();
	$res = $db_love_bless->insert_love_bless($current_uid,$g_cgival["request_id"],$text);
	if(!$res)
	{
		throw new Exception("sorry 数据库出错了");
	}
	

	$send_res = $c->repost($g_cgival["open_id"],$text);
	
	$send_res = $c->send_comment($g_cgival["open_id"],$text);
	
			
		
	
	
}
try {
	
	check_cgi_pro();
	get_data();
	$g_show["error"] = 0;
	$g_show["errmsg"] = "";
	
	
} 
catch (Exception $e) 
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
	
}
show_pro();

exit;

function show_pro()
{
	global $g_cgival, $g_pro, $g_show, $config;

	echo json_encode($g_show);

	unset($g_cgival);
	unset($g_pro);
	unset($g_show);
	unset($config);
}



?>